package com.example.demo.controller;


import com.example.demo.api.CommonResult;
import com.example.demo.api.ResultCode;
import com.example.demo.entity.Info;
import com.example.demo.entity.Report;
import com.example.demo.entity.User;
import com.example.demo.service.AdminService;
import com.example.demo.service.TokenService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/house/v1")
public class AdminCrontroller {

    @Autowired
    private AdminService adminservice;
    @Autowired
    private UserService userservice;
    @Autowired
    private TokenService tokenservice;


    /**
     * 管理员查看所有举报信息
     */
    @GetMapping("/reportinfo/{username}/{token}")
    public CommonResult browseReport (@PathVariable(name="username") String username,
                                      @PathVariable(name="token") String token) {
        System.out.println("......管理员查看所有举报信息......");
        List<Info> info;
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                info = adminservice.findAllReport();
                return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), info);
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }

    /**
     * 管理员查看某条举报信息
     */
    @GetMapping("/reportinfo/{username}/{token}/{id}")
    public CommonResult deleteAReport (@PathVariable(name="username") String username,
                                     @PathVariable(name="token") String token,
                                     @PathVariable(name="id") int id) {
        System.out.println("......查看单个举报信息......");
        Info info;
        Report report;
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                info = adminservice.findAReportInfo(id);
                report = adminservice.findAReportContent(id);
                System.out.println(report);
                return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), info, report);
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }


    /**
     * 管理员删除举报
     */
    @DeleteMapping("/reportinfo/{username}/{token}/{id}")
    public CommonResult lookAReport (@PathVariable(name="username") String username,
                                     @PathVariable(name="token") String token,
                                     @PathVariable(name="id") int info_id) {
        System.out.println("......删除举报信息......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                adminservice.deleteAReport(info_id);
                return CommonResult.result(ResultCode.DELETE_INFO_SUCCESS.getCode(), ResultCode.DELETE_INFO_SUCCESS.getMessage());
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.DELETE_INFO_FAILED.getCode(), ResultCode.DELETE_INFO_FAILED.getMessage());
        }
    }


    /**
     * 管理员封禁用户
     */
    @ResponseBody
    @PutMapping("/users/{username}/{token}")
    public CommonResult banUser (@PathVariable(name="username") String username,
                                 @PathVariable(name="token") String token,
                                 @RequestBody User username2) {
        System.out.println("......管理员封禁用户......");
        System.out.println(username2.getUsername());
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                if (userservice.findAUsers(username).getAdmin()) {
                    int i = adminservice.bannedUser(username2.getUsername());
                    System.out.println(i);
                    return CommonResult.result(ResultCode.BAN_USER_SUCCESS.getCode(), ResultCode.BAN_USER_SUCCESS.getMessage());
                } else{
                    return CommonResult.result(ResultCode.Admin_FAILED.getCode(), ResultCode.Admin_FAILED.getMessage());
                }
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.BAN_USER_FAILED.getCode(), ResultCode.BANNED_FAILED.getMessage());
        }
    }

    /**
     * 管理员解封用户
     */
    @ResponseBody
    @PostMapping("/users/{username}/{token}")
    public CommonResult unbanUser (@PathVariable(name="username") String username,
                                   @PathVariable(name="token") String token,
                                   @RequestBody User username2) {
        System.out.println(username2.getUsername());
        System.out.println("......管理员解封用户......");
        try {
            if (tokenservice.searchToken(token) && username.equals(tokenservice.getName(token))) {
                if (userservice.findAUsers(username).getAdmin()) {
                    int i = adminservice.unbannedUser(username2.getUsername());
                    System.out.println(i);
                    return CommonResult.result(ResultCode.UNBAN_USER_SUCCESS.getCode(), ResultCode.UNBAN_USER_SUCCESS.getMessage());
                } else{
                    return CommonResult.result(ResultCode.Admin_FAILED.getCode(), ResultCode.Admin_FAILED.getMessage());
                }
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.UNBAN_USER_FAILED.getCode(), ResultCode.UNBAN_USER_FAILED.getMessage());
        }
    }



    /**
     * 删除违规消息
     */
    @DeleteMapping("/info/{username}/{token}/{id}")
    public CommonResult deleteInfo(@PathVariable String id,
                                   @PathVariable(name="token") String token,
                                   @PathVariable(name="username") String username) {
        System.out.println("......删除违规消息......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                int _id = Integer.valueOf(id);
                adminservice.deleteAReport(_id);
                adminservice.deleteAInfo(_id);
                return CommonResult.result(ResultCode.DELETE_INFO_SUCCESS.getCode(), ResultCode.DELETE_INFO_SUCCESS.getMessage());
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.DELETE_INFO_FAILED.getCode(), ResultCode.DELETE_INFO_FAILED.getMessage());
        }
    }

    /**
     * 返回所有未封禁的用户
     */
    @GetMapping("/unban/{username}/{token}")
    public CommonResult allUnban(@PathVariable(name="token") String token,
                                 @PathVariable(name="username") String username) {
        System.out.println("......返回所有未封禁的用户......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                if (userservice.findAUsers(username).getAdmin()) {
                    List<User> users = adminservice.findAllUnban();
                    return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), users, token);
                }else {
                    return CommonResult.result(ResultCode.Admin_FAILED.getCode(), ResultCode.Admin_FAILED.getMessage());
                }
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }

    }


    /**
     * 返回所有已封禁的用户
     */
    @GetMapping("/ban/{username}/{token}")
    public CommonResult allBan(@PathVariable(name="token") String token,
                               @PathVariable(name="username") String username) {
        System.out.println("......返回所有已封禁的用户......");
        try {
            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
                if (userservice.findAUsers(username).getAdmin()) {
                    List<User> users = adminservice.findAllBan();
                    return CommonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), users, token);
                }else{
                    return CommonResult.result(ResultCode.Admin_FAILED.getCode(), ResultCode.Admin_FAILED.getMessage());
                }
            } else {
                return CommonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            return CommonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }



}
